package com.lagou.service.impI;

import com.lagou.base.StatusCode;
import com.lagou.dao.CourseContentDao;
import com.lagou.dao.impI.CourseContentDaoImpI;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.utils.DateUtils;

import java.util.List;

/**
 * @Author wsl
 * @Date 2021/10/11 19:51
 * @Project_Name maven
 *
 * 课程内容管理模块 Service层 的实现类
 */
public class CourseContentServiceImpI implements CourseContentService {

    CourseContentDao contentDao = new CourseContentDaoImpI();

    //1.根据课程ID查询课程相关的信息
    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {

        List<Course_Section> sections = contentDao.findSectionAndLessonByCourseId(courseId);
        return sections;
    }

    //3.根据课程ID 回显课程信息
    @Override
    public Course findCourseByCourseId(int courseId) {

        Course course = contentDao.findCourseByCourseId(courseId);
        return course;
    }

    //4.保存章节信息
    @Override
    public String saveSection(Course_Section section) {

        //1.补全章节信息
        section.setStatus(2);////0-隐藏，1-未发布，2-已发布
        String formart = DateUtils.getDateFormart();
        section.setCreate_time(formart);
        section.setUpdate_time(formart);

        //2.调用dao
        int row = contentDao.saveSection(section);

        //3.判断是否保存成功，封装对应信息
        if (row > 0){
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        } else {
            //保存失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    //5.修改章节信息
    @Override
    public String updateSection(Course_Section section) {

        //1.补全信息
        String formart = DateUtils.getDateFormart();
        section.setUpdate_time(formart);

        //2.调用dao
        int row = contentDao.updateSection(section);

        //3.判断是否修改成功
        if (row > 0){
            //修改成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        } else {
            //修改失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    //6.修改章节的状态
    @Override
    public String updateSectionStatus(int id, int status) {

        int row = contentDao.updateSectionStatus(id, status);

        //判断是否修改状态成功
        if (row > 0){
            //修改状态成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        } else {
            //修改状态失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    //7.保存课时信息
    @Override
    public String saveLesson(Course_Lesson lesson) {

        //1.补全信息
        lesson.setStatus(2);//0-隐藏，1-未发布，2-已发布
        String formart = DateUtils.getDateFormart();
        lesson.setCreate_time(formart);
        lesson.setUpdate_time(formart);

        //2.调用dao
        int row = contentDao.saveLesson(lesson);

        //3.判断是否保存课时信息成功
        if (row > 0){
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        } else {
            //保存失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    //8.修改课时信息
    @Override
    public String updateLesson(Course_Lesson lesson) {

        //1.补全信息
        String formart = DateUtils.getDateFormart();
        lesson.setUpdate_time(formart);

        //2.调用dao
        int row = contentDao.updateLesson(lesson);

        //3.判断是否修改课时信息成功
        if (row > 0){
            //修改成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        } else {
            //修改失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }
}
